#include <stdio.h>
#include <CoOS.h>
#include "NUC123.h"
#include "shell.h"

void LED_Init(void)
{
	SYS_ResetModule(PWM03_RST);

	 /* set PWMS channel 0 output configuration */
        PWM_ConfigOutputChannel(PWMA, PWM_CH0, 2000, 98);
	 PWM_ConfigOutputChannel(PWMA, PWM_CH1, 2000, 98);
	 PWM_ConfigOutputChannel(PWMA, PWM_CH2, 2000, 98);

        /* Enable PWM Output path for PWMA channel 0 */
        PWM_EnableOutput(PWMA, 0x1 | 0x02 |  0x04);

        /* Enable Timer for PWMA channel 0 */
        PWM_Start(PWMA, 0x1 | 0x02 |  0x04);
}

void LED_SetLevel(U8 u8Idx, U8 u8Level)
{
	if(u8Level >= 100)
	{
		u8Level = 99;
	
	}
	 PWM_ConfigOutputChannel(PWMA, u8Idx, 2000, 100 - u8Level);
}


void led_level(char *p0, char *p1, char *p2)
{
	U8 u8Level = atoi(p0);
	printf("Set level %d\n",u8Level);
	LED_SetLevel(0,u8Level);
	LED_SetLevel(1,u8Level);
	LED_SetLevel(2,u8Level);
}

SHELL_CMD(led, led_level);
